<template>
	<view>
		<swiper class="swiper" indicator-dots autoplay circular >
			<swiper-item v-for="(item,index) in pics" :key="index">
				<image :src="item.pics_big_url" />
			</swiper-item>
		</swiper>
		<van-card :price="goods_price" desc="描述信息" :title="goods_name" />
		<rich-text :nodes="goods_introduce"></rich-text>
		<!-- 弹框 -->
		<van-popup :show="show" position="bottom" custom-style="height: 50%;" @close="onClose" closeable>
			<van-card :price="goods_price" desc="描述信息" :title="goods_name" />
			<van-stepper :value="num" @change="onChange" />
			<van-button type="danger" @tap="addcart">添加</van-button>
		</van-popup>
		<!-- 底部 -->
		<van-goods-action>
			<van-goods-action-icon icon="chat-o" text="客服" />
			<van-goods-action-icon icon="cart-o" text="购物车" info="5" @tap="ccc"/>
			<van-goods-action-button color="#be99ff" text="加入购物车" type="warning" @tap="ttt" />
			<van-goods-action-button color="#7232dd" text="立即购买" />
		</van-goods-action>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [],
				pics: [],
				goods_small_logo: '',
				goods_price: '',
				goods_name: '',
				goods_introduce: [],
				goods_id: '',
				attrs: [],
				show: false,
				num: 1
			};
		},
		onLoad(e) {
			console.log(e)
			this.getlist(e.id)
		},
		methods: {
			async getlist(id) {
				let {
					data: res
				} = await uni.$http.get("/api/public/v1/goods/detail?goods_id=" + id)
				console.log(res)
				this.list = res.message
				this.pics = res.message.pics
				this.goods_small_logo = res.message.goods_small_logo
				this.goods_price = res.message.goods_price
				this.goods_name = res.message.goods_name
				this.goods_introduce = res.message.goods_introduce
				this.goods_id = res.message.goods_id
				this.attrs = res.message.attrs
			},
			onChange(e) {
				this.num = e.detail
			},
			onClose() {
				this.show = false
			},
			ttt() {
				this.show = true
			},
			addcart() {
				var cart = wx.getStorageSync('cart') || []
				var index = cart.findIndex(item => {
					return item.goods_id == this.goods_id
				})
				if (index == -1) {
					var obj = {
						goods_name: this.goods_name,
						goods_number: this.num,
						goods_price: this.goods_price,
						goods_small_logo: this.goods_small_logo,
						goods_id: this.goods_id

					}
					cart.unshift(obj)
				} else {
					cart[index].goods_number += this.num
				}
				wx.setStorageSync('cart', cart)
				wx.showToast({
					title: '成功',
					icon: 'success',
				})
				this.show = false
			},
			ccc(){
				wx.switchTab({
					url:'/pages/cart/cart'
				})
			}
		}
	}
</script>

<style lang="scss">
	.swiper {
		width: 750rpx;
		height: 750rpx;

		image {
			width: 750rpx;
			height: 750rpx;
		}
	}
</style>
